package com.xiyun.admin.controller;


import com.xiyun.common.response.result.R;
import com.xiyun.admin.entity.SubjectChapter;
import com.xiyun.admin.entity.vo.ChapterVo;
import com.xiyun.admin.service.SubjectChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-16
 */
@CrossOrigin
@Api(description = "课程章节管理")
@RestController
@RequestMapping("/xiyun/service/subject/chapter")
public class SubjectChapterController {

    @Autowired
    private SubjectChapterService chapterService;

    /**
     * 课程大纲表,根据课程id进行查询
     * @param subjectId 课程id
     * @return  响应结果
     */
    @ApiOperation("获取课程大纲列表")
    @GetMapping("getChapterVideo/{subjectId}")
    public R getChapterVideo(@PathVariable String subjectId){
        List<ChapterVo> list = chapterService.getChapterVideoBySubjectId(subjectId);
        return R.ok().data("allChapterVideo",list);
    }

    @ApiOperation("添加章节信息")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody SubjectChapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation("根据章节id查询章节信息")
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfoById(@PathVariable String chapterId){
        SubjectChapter subjectChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",subjectChapter);
    }

    @ApiOperation("修改章节信息")
    @PutMapping("updateChapter")
    public R updateChapter(@RequestBody SubjectChapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation("删除章节信息")
    @GetMapping("deleteChapter/{chapterId}")
    public R delChapter(@PathVariable String chapterId){
        return chapterService.deleteChapter(chapterId) ? R.ok() : R.error().message("当前章节中包含小节,无法删除");
    }
}

